博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
邮件发送
阅读量:5278 次
发布时间:2019-06-14

本文共 6981 字,大约阅读时间需要 23 分钟。

1:整合springboot的发送参见:

https://blog.csdn.net/zyw_java/article/details/81635375

2:常见的邮箱服务器地址:

https://blog.csdn.net/chuanyu/article/details/46740287

3:如何查询公司的outlook邮箱服务器地址:

https://zhidao.baidu.com/question/39774692.html

 

关键点:

密码为三方授权码

待解决问题:

如何查询企业邮箱的服务地址?

 

demo

org.springframework.boot
spring-boot-starter-mail

配置

spring:  application:    name: test-config-server  profiles:  mail:    host: smtp.163.com #发送邮件服务器    username: ppyun7c@163.com #163邮箱    password: wdsqm01 #客户端授权码    protocol: smtp #发送邮件协议    properties.mail.smtp.auth: true    properties.mail.smtp.port: 465 #端口号465或994    properties.mail.display.sendmail: XIEDONGXU #可以任意    properties.mail.display.sendname: Spring Boot Guide Email #可以任意    properties.mail.smtp.starttls.enable: true    properties.mail.smtp.starttls.required: true    properties.mail.smtp.ssl.enable: true    default-encoding: utf-8    from: ppyun7c@163.com #与上面的username保持一致

 

代码

package com.test.domi.service;import javax.mail.MessagingException;public interface IMailService {    /**     * 发送文本邮件     * @param to     * @param subject     * @param content     */    public void sendSimpleMail(String to, String subject, String content, String... cc);    /**     * 发送HTML邮件     * @param to     * @param subject     * @param content     * @throws MessagingException     */    public void sendHtmlMail(String to, String subject, String content, String... cc)throws MessagingException;    /**     * 发送带附件的邮件     * @param to     * @param subject     * @param content     * @param filePath     * @throws MessagingException     */    public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc)throws MessagingException;    /**     * 发送正文中有静态资源的邮件     * @param to     * @param subject     * @param content     * @param rscPath     * @param rscId     * @throws MessagingException     */    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc)throws MessagingException;}

 实现类

package com.test.domi.service.impl;import com.test.domi.service.IMailService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.io.FileSystemResource;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Component;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import java.io.File;@Componentpublic class IMailServiceImpl implements IMailService {    @Autowired    private JavaMailSender mailSender;    @Value("${spring.mail.from}")    private String from;    /**     * 发送文本邮件     * @param to     * @param subject     * @param content     */    @Override    public void sendSimpleMail(String to, String subject, String content, String... cc) {        SimpleMailMessage message = new SimpleMailMessage();        message.setFrom(from);        message.setTo(to);        message.setCc(cc);        message.setSubject(subject);        message.setText(content);        mailSender.send(message);    }    /**     * 发送HTML邮件     * @param to     * @param subject     * @param content     */    @Override    public void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException{        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper helper = new MimeMessageHelper(message, true);        helper.setFrom(from);        helper.setTo(to);        helper.setCc(cc);        helper.setSubject(subject);        helper.setText(content, true);        mailSender.send(message);    }    /**     * 发送带附件的邮件     * @param to     * @param subject     * @param content     * @param filePath     * @throws MessagingException     */    @Override    public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc)throws MessagingException {        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper helper = new MimeMessageHelper(message, true);        helper.setFrom(from);        helper.setTo(to);        helper.setCc(cc);        helper.setSubject(subject);        helper.setText(content, true);        FileSystemResource file = new FileSystemResource(new File(filePath));        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));        helper.addAttachment(fileName, file);        mailSender.send(message);    }    /**     * 发送正文中有静态资源的邮件     * @param to     * @param subject     * @param content     * @param rscPath     * @param rscId     */    @Override    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException {        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper helper = new MimeMessageHelper(message, true);        helper.setFrom(from);        helper.setTo(to);        helper.setCc(cc);        helper.setSubject(subject);        helper.setText(content, true);        FileSystemResource res = new FileSystemResource(new File(rscPath));        helper.addInline(rscId, res);        mailSender.send(message);    }}

 

 

controller

package com.test.domi.controller;import com.test.domi.common.utils.ResultInfo;import com.test.domi.common.utils.ResultUtil;import com.test.domi.service.impl.IMailServiceImpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/email")public class EmailController {    @Autowired    private IMailServiceImpl mailService;//注入发送邮件的各种实现方法    @GetMapping("/normal")    public ResultInfo index(){        try {            mailService.sendSimpleMail("491431567@qq.com","SpringBoot Email","这是一封普通的SpringBoot测试邮件");            String[] cc = {"491431567@qq.com","491431567@qq.com"};            mailService.sendAttachmentsMail("491431567@qq.com","附件发送测试","这是附件","C:\\Users\\Domi\\Desktop\\ls.txt",cc);            mailService.sendHtmlMail("491431567@qq.com","html测试","\n"                    + "    
\n" +"

欢迎使用IJPay -By Javen

\n" +"
https://github.com/Javen205/IJPay" + "
IJPay 让支付触手可及,欢迎Start支持项目发展:)
\n" + "
如果对你有帮助,请任意打赏\n" + "
\n" + " \n" + "",cc); }catch (Exception ex){ ex.printStackTrace(); return ResultUtil.getFailResult("999999","失败",null); } return ResultUtil.getSuccessResult(null); }}

 

转载于:https://www.cnblogs.com/domi22/p/9749024.html

你可能感兴趣的文章
对SPI、IIC、IIS、UART、CAN、SDIO、GPIO的解释
查看>>
Thymeleaf模板格式化LocalDatetime时间格式
查看>>
庖丁解“学生信息管理系统”
查看>>
Pyltp使用
查看>>
其他ip无法访问Yii的gii,配置ip就可以
查看>>
js创建对象
查看>>
使用json格式输出
查看>>
border-image属性在chrome中的不同效果
查看>>
php做的一个简易爬虫
查看>>
x的x次幂的值为10,求x的近似值
查看>>
hdu-5009-Paint Pearls-dp
查看>>
内存泄漏调查
查看>>
jquery获取html元素的绝对位置和相对位置的方法
查看>>
谈谈spring
查看>>
ios中webservice报文的拼接
查看>>
Power BI 报告的评论服务支持移动设备
查看>>
HDU 4920 Matrix multiplication
查看>>
ACdream 1068
查看>>
会声会影毛玻璃制作
查看>>
HDU 2665 Kth number
查看>>